<!DOCTYPE html>   
<head>   
<meta charset="UTF-8">  
<title>使用Web SQL设计Web留言本</title>
<style type="text/css">
textarea { width: 100%; }
input[type="text"], input[type="password"] { width: 180px; height: 24px; line-height: 24px; }
input[type="submit"], input[type="button"] { width: 80px; height: 24px; line-height: 24px; border: 1px solid #ff6600; border-radius: 4px; background: #ff6600; outline: none; color: #fff; cursor: pointer; margin-top: 6px; }
/*定义表格样式*/
#datatable  { border-collapse: collapse; width: 100%; font-size: 14px; color: #666; border: solid 1px #0047E1; }
/*定义列标题样式*/
#datatable  th { background: #0047E1; color: #fff; font-size: 16px; }
/*定义隔行背景色，改善视觉效果*/
#datatable  tr:nth-child(odd) { background: #eee; }
#datatable  tr:hover { background: #ddd; color: #000; }
</style>  
<script type="text/javascript">
var datatable = null;  
var db = openDatabase('MyData', '', 'My Database', 102400);  
function init()
{  
    datatable = document.getElementById("datatable");  
    showAllData();  
}  
function removeAllData()
{  
    for (var i =datatable.childNodes.length-1; i>=0; i--) 
    {  
        datatable.removeChild(datatable.childNodes[i]);  
    }  
    var tr = document.createElement('tr');  
    var th1 = document.createElement('th');  
    var th2 = document.createElement('th');  
    var th3 = document.createElement('th');  
    th1.innerHTML = '姓名';  
    th2.innerHTML = '留言';  
    th3.innerHTML = '时间';  
    tr.appendChild(th1);  
    tr.appendChild(th2);  
    tr.appendChild(th3);  
    datatable.appendChild(tr);  
}  
function showData(row) 
{  
    var tr = document.createElement('tr');  
    var td1 = document.createElement('td');  
    td1.innerHTML = row.name;  
    var td2 = document.createElement('td');  
    td2.innerHTML = row.message;  
    var td3 = document.createElement('td');  
    var t = new Date();  
    t.setTime(row.time);  
    td3.innerHTML=t.toLocaleDateString()+" "+t.toLocaleTimeString();  
    tr.appendChild(td1);  
    tr.appendChild(td2);  
    tr.appendChild(td3);  
    datatable.appendChild(tr);    
}  
function showAllData() 
{  
    db.transaction(function(tx) 
    {  
        tx.executeSql('CREATE TABLE IF NOT EXISTS MsgData(name TEXT, message TEXT, time INTEGER)',[]);  
        tx.executeSql('SELECT * FROM MsgData', [], function(tx, rs) 
        {  
            removeAllData();  
            for(var i = 0; i < rs.rows.length; i++) 
            {  
                showData(rs.rows.item(i));  
            }  
        });  
    });  
}  
function addData(name, message, time) 
{  

    db.transaction(function(tx) 
    {  
        tx.executeSql('INSERT INTO MsgData VALUES(?, ?, ?)',[name, message, time],function(tx, rs) 
        {  
            alert("成功保存数据!");  
        },  
        function(tx, error) 
        {  
            alert(error.source + "::" + error.message);  
        });  
    });  
}  
function saveData()
{  
    var name = document.getElementById('name').value;  
    var memo = document.getElementById('memo').value;  
    var time = new Date().getTime();  
    addData(name,memo,time);  
    showAllData();  
}
</script>  
</head>  
<body onload="init();">  
<h1>使用Web SQL设计Web留言本</h1>  
<table>  
    <tr><td>姓名:</td><td><input type="text" id="name"></td></tr>  
    <tr><td>留言:</td><td><input type="text" id="memo"></td></tr>  
    <tr>
<td></td>
<td><input type="button" value="保存" onclick="saveData();"></td>
</tr>  
</table>  
<hr>  
<table id="datatable" border="1"></table>  
<p id="msg"></p>  
</body>  
</html>  
